"""
@-*- coding: utf-8 -*-
@ python：python 3.8
@ 创建人员：ylx
@ 创建时间：2025/3/7 上午10:41
@ 内容：
"""
import requests
import logging
from common.yaml_util import *

logging.basicConfig(level=logging.INFO)     #配置日志记录


def base_url_edger():   #获取爱智的域名
    edger_url = "https://" + str(read_yaml('privateDomain'))
    return edger_url


class RequestUtil:
    """
    统一封装接口请求
    """
    sessions = requests.session()   #使用类变量，统一所有相关请求确保在一个会话
    base_url_cloud = "https://api.q.e0a.cc"   #默认云服务
    ca_cert_path = r"C:\Users\lenovo\Downloads\edgeros_root.crt"    #爱智的ssl证书

    @staticmethod
    def send_request(method, url, datas=None, content_type="json",  **kwargs):
        """
        :param method: 请求方式('get','post','put','delete')
        :param url: 请求地址
        :param datas: 请求入参("字典或json格式")，如果有，不需要的话，默认值为None
        :param content_type: 入参类型，默认为application/json
        :param kwargs: 其他参数，通过不定长关键字参数传入
        :return: 返回接口响应response
        """
        methods = str(method).lower()   #传入的method可能是大写，此处处理，统一转换为小写
        responses = None

        try:
            if methods == "get":
                params = datas if datas else {}     #使用条件赋值表达式，优化代码
                logging.info(f"Sending GET request to {url} with params: {params}")
                responses = RequestUtil.sessions.request(method=methods, url=url, params=params, **kwargs)
                # if datas:
                #     responses = RequestUtil.sessions.request(method=methods, url=url, params=datas)
                # else:
                #     responses = RequestUtil.sessions.request(method=methods, url=url)
            elif methods == "post":
                if content_type == 'json':
                    logging.info(f"Sending POST request to {url} with json data: {datas}")
                    responses = RequestUtil.sessions.request(method=methods, url=url, json=datas, **kwargs)
                else:
                    logging.info(f"Sending POST request to {url} with from data: {datas}")
                    responses = RequestUtil.sessions.request(method=methods, url=url, data=datas, **kwargs)
            elif methods == "put":
                if datas:
                    if content_type == 'json':
                        logging.info(f"Sending PUT request to {url} with json data: {datas}")
                        responses = RequestUtil.sessions.request(method=methods, url=url, json=datas, **kwargs)
                    else:
                        logging.info(f"Sending PUT request to {url} with from data: {datas}")
                        responses = RequestUtil.sessions.request(method=methods, url=url, data=datas, **kwargs)
                else:
                    logging.info(f"Sending PUT request to {url}")
                    responses = RequestUtil.sessions.request(method=methods, url=url, **kwargs)
            elif methods == "delete":
                if datas:
                    if content_type == 'json':
                        logging.info(f"Sending DELETE request to {url} with json data: {datas}")
                        responses = RequestUtil.sessions.request(method=methods, url=url, json=datas, **kwargs)
                    else:
                        logging.info(f"Sending DELETE request to {url} with from data: {datas}")
                        responses = RequestUtil.sessions.request(method=methods, url=url, data=datas, **kwargs)
                else:
                    logging.info(f"Sending DELETE request to {url}")
                    responses = RequestUtil.sessions.request(method=methods, url=url, **kwargs)
            else:
                raise ValueError(f"Unsupported HTTP method: {methods}")

            logging.info(f"Status Code: {responses.status_code}")
            logging.info(f"Response Body: {responses.text}")

            return responses

        except requests.exceptions.RequestException as e:
            logging.error(f"An error occurred: {e}")
            return None

